package org.farng.mp3.object;

import org.farng.mp3.TagUtility;

import java.util.ArrayList;
import java.util.Iterator;


/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2002
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 *
 * @author Eric Farng
 * @version $Revision: 1.8 $
 */
public class ObjectGroupRepeated extends AbstractMP3Object {
    /** DOCUMENT ME! */
    private ArrayList objectList;

    /** DOCUMENT ME! */
    private ArrayList propertyList;

    /**
     * Creates a new ObjectGroupRepeated object.
     *
     * @param identifier DOCUMENT ME!
     */
    public ObjectGroupRepeated(String identifier) {
        this.identifier   = identifier;
        this.propertyList = new ArrayList();
        this.objectList   = new ArrayList();
    }

    public ObjectGroupRepeated(ObjectGroupRepeated copyObject) {
        super(copyObject);

        AbstractMP3Object newObject;

        for (int i = 0; i < copyObject.objectList.size(); i++) {
            newObject = (AbstractMP3Object) TagUtility.copyObject(copyObject.objectList.get(i));
            this.objectList.add(newObject);
        }

        for (int i = 0; i < copyObject.propertyList.size(); i++) {
            newObject = (AbstractMP3Object) TagUtility.copyObject(copyObject.propertyList.get(i));
            this.propertyList.add(newObject);
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public ArrayList getObjectList() {
        return this.objectList;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public ArrayList getPropertyList() {
        return this.propertyList;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getSize() {
        int               size = 0;
        AbstractMP3Object object;
        Iterator          iterator = objectList.listIterator();

        while (iterator.hasNext()) {
            object = (AbstractMP3Object) iterator.next();
            size += object.getSize();
        }

        return size;
    }

    /**
     * DOCUMENT ME!
     *
     * @param obj DOCUMENT ME!
     */
    public void addObject(AbstractMP3Object obj) {
        objectList.add(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @param obj DOCUMENT ME!
     */
    public void addProperty(AbstractMP3Object obj) {
        propertyList.add(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @param obj DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean equals(Object obj) {
        if ((obj instanceof ObjectGroupRepeated) == false) {
            return false;
        }

        ObjectGroupRepeated object = (ObjectGroupRepeated) obj;

        if (this.objectList.equals(object.objectList) == false) {
            return false;
        }

        if (this.propertyList.equals(object.propertyList) == false) {
            return false;
        }

        return super.equals(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @param arr DOCUMENT ME!
     * @param offset DOCUMENT ME!
     *
     * @throws NullPointerException DOCUMENT ME!
     * @throws IndexOutOfBoundsException DOCUMENT ME!
     */
    public void readByteArray(byte[] arr, int offset) {
        if (arr == null) {
            throw new NullPointerException("Byte array is null");
        }

        if ((offset < 0) || (offset >= arr.length)) {
            throw new IndexOutOfBoundsException("Offset to byte array is out of bounds: offset = " + offset + ", array.length = " + arr.length);
        }

        AbstractMP3Object object;
        Class             className;
        Iterator          iterator;

        if (propertyList.size() > 0) {
            while (offset < arr.length) {
                iterator = propertyList.listIterator();

                while (iterator.hasNext()) {
                    className = iterator.next().getClass();

                    try {
                        object = (AbstractMP3Object) className.newInstance();
                        objectList.add(object);
                        object.readByteArray(arr, offset);
                        offset += object.getSize();
                    } catch (IllegalAccessException ex) {
                        ex.printStackTrace();

                        // do nothing, just skip this one
                    } catch (InstantiationException ex) {
                        ex.printStackTrace();

                        // do nothing, just skip this one
                    }
                }
            }
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String toString() {
        String            str = "";
        AbstractMP3Object object;
        Iterator          iterator = objectList.listIterator();

        while (iterator.hasNext()) {
            object = (AbstractMP3Object) iterator.next();
            str += (object.toString() + "\n");
        }

        return str;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public byte[] writeByteArray() {
        AbstractMP3Object object;
        byte[]            totalArray = new byte[this.getSize()];
        byte[]            objectArray;
        Iterator          iterator = objectList.listIterator();

        while (iterator.hasNext()) {
            object      = (AbstractMP3Object) iterator.next();
            objectArray = object.writeByteArray();
            System.arraycopy(objectArray, 0, totalArray, 0, totalArray.length);
        }

        return totalArray;
    }
}